﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace C_homework
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
            InitializeTreeView();
            InitializeListView();
        }

        // 初始化 TreeView，展示 C 盘目录结构
        private void InitializeTreeView()
        {
            treeView.AfterSelect += treeView1_AfterSelect;
            treeView.NodeMouseDoubleClick += treeView1_NodeMouseDoubleClick;

            TreeNode rootNode = new TreeNode("C:\\");
            rootNode.Tag = "C:\\";
            treeView.Nodes.Add(rootNode);

            LoadSubDirectories(rootNode);
        }

        // 初始化 ListView，并绑定双击事件
        private void InitializeListView()
        {
            listView.View = View.Details;
            listView.Columns.Add("名称", 300);
            listView.FullRowSelect = true;
            listView.ItemActivate += listView1_ItemActivate;
        }

        // 加载子目录到 TreeView
        private void LoadSubDirectories(TreeNode node)
        {
            try
            {
                string[] subDirectories = Directory.GetDirectories(node.Tag.ToString());
                foreach (string dir in subDirectories)
                {
                    TreeNode subNode = new TreeNode(Path.GetFileName(dir));
                    subNode.Tag = dir;
                    node.Nodes.Add(subNode);
                }
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("无权访问该目录。");
            }
        }

        // TreeView 节点选择事件：在 ListView 中展示目录内容
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string path = e.Node.Tag.ToString();
            LoadFilesAndDirectories(path);
        }

        // ListView 双击事件：进入子目录或打开文件
        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            string filePath = listView.SelectedItems[0].Tag.ToString();

            if (Directory.Exists(filePath)) // 如果是目录，加载目录内容
            {
                LoadFilesAndDirectories(filePath);
            }
            else // 如果是文件，根据类型执行操作
            {
                OpenFile(filePath);
            }
        }

        // TreeView 双击事件：展开节点并加载子目录
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Nodes.Count == 0) // 如果还没有加载子节点
            {
                LoadSubDirectories(e.Node);
            }
            e.Node.Expand(); // 展开节点
        }

        // 加载目录中的文件和文件夹到 ListView
        private void LoadFilesAndDirectories(string path)
        {
            listView.Items.Clear();

            try
            {
                // 加载子目录
                foreach (var dir in Directory.GetDirectories(path))
                {
                    ListViewItem dirItem = new ListViewItem(Path.GetFileName(dir));
                    dirItem.Tag = dir;
                    dirItem.ImageIndex = 0; // 设置为文件夹图标索引
                    listView.Items.Add(dirItem);
                }

                // 加载文件
                foreach (var file in Directory.GetFiles(path))
                {
                    ListViewItem fileItem = new ListViewItem(Path.GetFileName(file));
                    fileItem.Tag = file;
                    fileItem.ImageIndex = 1; // 设置为文件图标索引
                    listView.Items.Add(fileItem);
                }
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("无权访问该目录。");
            }
        }

        // 根据文件类型打开文件
        private void OpenFile(string filePath)
        {
            string extension = Path.GetExtension(filePath).ToLower();

            try
            {
                if (extension == ".exe") // 运行 exe 文件
                {
                    Process.Start(filePath);
                }
                else if (extension == ".txt") // 用记事本打开 txt 文件
                {
                    Process.Start("notepad.exe", filePath);
                }
                else
                {
                    MessageBox.Show("不支持该文件类型的操作。");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开文件时出错：{ex.Message}");
            }
        }
    }

}

